﻿using System;
public interface IShape
{
    double CalculateArea();
    bool IsValid();
}

public class Rectangle : IShape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public double CalculateArea()
    {
        return Length * Width;
    }

    public bool IsValid()
    {
        return Length > 0 && Width > 0;
    }
}

public class Square : IShape
{
    public double Side { get; set; }

    public Square(double side)
    {
        Side = side;
    }

    public double CalculateArea()
    {
        return Side * Side;
    }

    public bool IsValid()
    {
        return Side > 0;
    }
}
public class Triangle : IShape
{
    public double SideA { get; set; }
    public double SideB { get; set; }
    public double SideC { get; set; }

    public Triangle(double sideA, double sideB, double sideC)
    {
        SideA = sideA;
        SideB = sideB;
        SideC = sideC;
    }

    public double CalculateArea()
    {
        double s = (SideA + SideB + SideC) / 2; 
        return Math.Sqrt(s * (s - SideA) * (s - SideB) * (s - SideC));
    }

    public bool IsValid()
    {
        return SideA > 0 && SideB > 0 && SideC > 0 &&
               SideA + SideB > SideC &&
               SideA + SideC > SideB &&
               SideB + SideC > SideA;
    }
}
public static class ShapeFactory
{
    private static Random random = new Random();
    public static IShape CreateRandomShape()
    {
        int shapeType = random.Next(0, 3); // 0、1、2，分别代表长方形、正方形、三角形

        switch (shapeType)
        {
            case 0: // 长方形
                double length = random.Next(1, 10);
                double width = random.Next(1, 10);
                return new Rectangle(length, width);

            case 1: // 正方形
                double side = random.Next(1, 10);
                return new Square(side);

            case 2: // 三角形
                double sideA = random.Next(1, 10);
                double sideB = random.Next(1, 10);
                double sideC = random.Next((int)(sideA-sideB+1), (int)(sideA + sideB)); 
                return new Triangle(sideA, sideB, sideC);

            default:
                throw new InvalidOperationException("未知的形状类型。");
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        double totalArea = 0;
        for (int i = 0; i < 10; i++)
        {
            IShape shape = ShapeFactory.CreateRandomShape();
            double area = shape.CalculateArea();
            totalArea += area;

            Console.WriteLine($"创建的形状: {shape.GetType().Name}, 面积: {area:F2}");
        }

        Console.WriteLine($"\n所有形状的总面积: {totalArea:F2}");
    }
}